package com.ruoyi.system.project;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Sequence;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.domain.ProjectFile;
import com.ruoyi.system.domain.ProjectManagement;
import com.ruoyi.system.domain.ProjectResearch;
import com.ruoyi.system.domain.ProjectSchedule;
import com.ruoyi.system.service.IProjectManagementService;
import com.ruoyi.system.service.IProjectScheduleService;
import org.activiti.engine.delegate.BaseTaskListener;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Map;

/**
 * @author zhaonz
 * @version 1.0
 * @date 2023/6/9 9:10
 */

@Service
public class TaskListenerExpression implements Serializable {

    private static final Logger log = LoggerFactory.getLogger(TaskListenerExpression.class);

    @Autowired
    private IProjectManagementService projectManagementService;

    @Autowired
    private IProjectScheduleService projectScheduleService;

    public void create(DelegateTask delegateTask) {

        log.info("数据库中的taskId主键: {}", delegateTask.getId());

        log.info("creattime: {}", delegateTask.getCreateTime());

        log.info("getProcessInstanceId: {}", delegateTask.getProcessInstanceId());


        log.info("数据库中的taskId主键: {}", delegateTask.getId());
        log.info("任务名称: {}", delegateTask.getName());
        delegateTask.setName("修改任务名称");

        log.info("获取任务的描述信息: {}", delegateTask.getDescription());
        delegateTask.setDescription("修改任务的描述信息");
        /**
         * lower priority: [0..19] lowest, [20..39] low, [40..59] normal, [60..79] high
         * [80..100] highest
         任务处理的优先级范围是0-100
         */
        log.info("任务处理的优先级范围是0-100: {}", delegateTask.getPriority());
        delegateTask.setPriority(1); /** 修改优先级*/

        log.info("获取流程实例id: {}", delegateTask.getProcessInstanceId());
        log.info("获取流程获取执行id: {}", delegateTask.getExecutionId());
        log.info("获取流程定义id: {}", delegateTask.getProcessDefinitionId());


        /** 添一个加候选人 */
        //void addCandidateUser(String userId);

        /** 添加候选人集合 */
        //void addCandidateUsers(Collection<String> candidateUsers);

        /** 添加候选组 */
        //void addCandidateGroup(String groupId);


        String eventName = delegateTask.getEventName();
        log.info(eventName);
    }

    public void assignment(DelegateTask delegateTask) {

        Map<String, Object> variables = delegateTask.getVariables();
        Long projectId = (Long) variables.get("projectId");
        String projectSchedule = DictUtils.getDictValue("sys_project_schedule", delegateTask.getName());

        log.info("任务名称: {}, {}", delegateTask.getName(), projectSchedule);

        projectManagementService.update(new UpdateWrapper<ProjectManagement>()
                .eq("project_id", projectId)
                .set("project_schedule", projectSchedule));

        ProjectSchedule schedule = new ProjectSchedule()
                .setScheduleId(new Sequence(1, 1).nextId())
                .setProjectId(projectId).setIsWithFiles(0)
                .setState("create")
                .setProjectSchedule(projectSchedule);

        delegateTask.setVariable("scheduleId", schedule.getScheduleId());
        projectScheduleService.save(schedule);
    }

    public void complete(DelegateTask delegateTask) {

        Map<String, Object> variables = delegateTask.getVariables();
        Long scheduleId = (Long) variables.get("scheduleId");
        log.info("任务名称: {}", delegateTask.getEventName());

        projectScheduleService.update(new UpdateWrapper<ProjectSchedule>()
                .eq("schedule_id", scheduleId)
                .set("state", "complete"));
    }

    public void end(DelegateExecution delegateExecution) {

        Map<String, Object> variables = delegateExecution.getVariables();
        Long projectId = (Long) variables.get("projectId");
        log.info("任务名称: {}", delegateExecution.getEventName());

        projectManagementService.update(new UpdateWrapper<ProjectManagement>()
                .eq("project_id", projectId)
                .set("project_schedule", "END"));
    }
}
